<?php
/**
 * 
 * @author Paolo Bertuzzo
 *
 */
namespace User\Service;

use Catalog\Data\BasketDao;
use Utilities\Session\SessionFacade;
use Catalog\Data\BasketItemDao;
use User\Data\OrderHeaderDao;
use Utilities;
use User\Data\OrderItemDao;
use User\Data\OrderAddressDao;
use User\Data\CustomerAddressDao;

class OrderService
{
    
    /**
     *
     */
    protected $basketDao;
    
    /**
     *
     */
    protected $basketItemDao;

    /**
     *
     */
    protected $orderHeaderDao;
    
    /**
     *
     */
    protected $orderItemDao;
    
    /**
     *
     */
    protected $orderAddressDao;

    /**
     *
     */
    protected $customerAddressDao;
    
    /**
     * 
     * @param BasketDao $basketDao
     * @param BasketItemDao $basketItemDao
     * @param OrderHeaderDao $orderHeaderDao
     * @param OrderItemDao $orderItemDao
     * @param OrderAddressDao $orderAddressDao
     * @param CustomerAddressDao $customerAddressDao
     */
    public function __construct(BasketDao $basketDao, BasketItemDao $basketItemDao, OrderHeaderDao $orderHeaderDao, OrderItemDao $orderItemDao,
        OrderAddressDao $orderAddressDao, CustomerAddressDao $customerAddressDao)
    {
    	$this->basketDao = $basketDao;
    	$this->basketItemDao = $basketItemDao;
    	$this->orderHeaderDao = $orderHeaderDao;
    	$this->orderItemDao = $orderItemDao;
    	$this->orderAddressDao = $orderAddressDao;
    	$this->customerAddressDao = $customerAddressDao;
    }
    
    /**
     * 
     */
    public function createOrderFromBasket() 
    {
        $sessionFacade = new SessionFacade();
        
        // Get the basket
    	$basket = $this->basketDao->get($sessionFacade->getSessionId(), $sessionFacade->getStore()->getId())[0];
    	
    	// Insert the order header
    	$total = $basket->getTotal();
    	$vat = $this->getVat($basket->getId());
    	$totalWithoutVat = $total - $vat;
    	$shippingTax = $this->getShippingTax($basket->getSessionId());
    	
    	$orderHeader = $this->orderHeaderDao->insert($sessionFacade->getCustomer()->getId(), $sessionFacade->getCustomer()->getFirstName(),
    	    $sessionFacade->getCustomer()->getLastName(), $sessionFacade->getCustomer()->getEmail(),
    	    'not_payed', $total, $vat, $totalWithoutVat, $shippingTax, new \DateTime());

    	// Insert the order address
    	$addressTypes = [ 'billing', 'shipping' ];
    	
    	foreach ($addressTypes as $addressType) {
        	$address = $this->customerAddressDao->getByType($sessionFacade->getCustomer()->getId(), $addressType);
        	
        	$this->orderAddressDao->insert($orderHeader->getId(), $address->getType(), $address->getStreet(), 
        	    $address->getNumber(), $address->getCity(), $address->getZipCode(), $address->getState(), $address->getCountry());
    	}
    	
    	// Insert the order items
    	$items = $this->basketItemDao->get($basket->getId());
    	
    	foreach ($items as $item)
    	    $this->orderItemDao->insert($orderHeader->getId(), $item->getProductId(), $item->getDescription(), $item->getAttributes(), 
    	        $item->getPrice(), $item->getVat(), $item->getQuantity());
    	
    	return $orderHeader;
    }
    
    /**
     * 
     * @param integer $basketId
     * @return number
     */
    public function getVat($basketId) 
    {
    	$vatTotals = $this->basketItemDao->getVatTotals($basketId);
        
        $vat = 0;

        foreach ($vatTotals as $vatTotal)
        	$vat += Utilities\Math\Vat::calculateVat($vatTotal['total'], $vatTotal['vat']);
        
        return $vat;
    }

    /**
     *
     * @param string $sessionId
     * @return number
     */
    public function getShippingTax($sessionId)
    {
        return $this->basketDao->getShippingTax($sessionId);
    }
    
}

?>